<?php
namespace app\api\controller\v1\attachment;
use app\api\controller\v1\Init;

class Attachment extends Init
{
	public function _initialize() {
		parent::_initialize();
		$this->model = model('attachment/attachment', 'service');
	}

	public function lists(){
		$maps = [];
		if($this->params['keyword']) {
			$maps['nickname|province|city|country'] = ['like', '%'.$this->params['keyword'].'%'];
		}
		$info = $this->model->lists($maps, $this->params['limit'], $this->params['page']);
		return $this->response(lang('Operation completed'), $info);
	}

	public function info() {
		$maps = [];
		$maps['id'] = $this->params['id'];
		$info = $this->model->get($maps);
		return $this->response(lang('Operation completed'), $info);
	}

	public function add() {
		$post = $this->request->post();
		$result = $this->model->add($post, [], 'add');
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	public function edit() {
		$post = $this->request->post();
		$result = $this->model->edit($post, ['id' => $post['id']], 'edit');
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	public function delete() {
		$id = $this->request->post('id/s');
		$ids = explode(",", $id);
		$maps = ['id' => ['in', $ids]];
		$result = $this->model->where($maps)->delete();
		if($result === false) {
			return $this->response($this->model->getError());
		}
		return $this->response(lang('Operation completed'), $result);
	}

	//文件上传接口
	public function upload() {
		$file = $this->request->file('file');
        $result = $this->model->upload($file, $this->params['folder']);
        if($result === false) {
			return $this->response($this->model->getError(), $result, -21001);
		}
		return $this->response(lang('Operation completed'), $result);
	}

}